<template>
    <div class="split-login-page-container">
        <div class="split-login-wrapper">
            <!-- 左侧表单区域 -->
            <div class="split-login-form-section">
                <el-form ref="loginFormRef" :model="loginForm" :rules="rules" class="retained-login-form" auto-complete="on"
                    label-position="left">
                    <div class="form-title-container">
                        <h3 class="form-title-text">欢迎登录</h3>
                    </div>
                    <el-form-item prop="username">
                        <el-input ref="username" v-model="loginForm.username" placeholder="请输入用户名" name="username" type="text"
                            tabindex="1" auto-complete="on" size="large">
                            <template #prefix>
                                <el-icon><User /></el-icon>
                            </template>
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
                            placeholder="请输入密码" name="password" tabindex="2" auto-complete="on"
                            @keyup.enter="handleLogin" size="large">
                            <template #prefix>
                                <el-icon><Lock /></el-icon>
                            </template>
                            <template #suffix>
                                <span class="show-pwd-icon" @click="showPwd">
                                    <el-icon><View v-if="passwordType === 'password'" /><Hide v-else /></el-icon>
                                </span>
                            </template>
                        </el-input>
                    </el-form-item>
                    <el-form-item class="user-type-radio-group">
                        <el-radio-group v-model="loginForm.type">
                            <el-radio :label="1">管理员</el-radio>
                            <el-radio :label="2">教师</el-radio>
                            <el-radio :label="3">学生</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-button :loading="loading" type="primary" class="retained-login-button" size="large"
                        @click.prevent="handleLogin">登 录</el-button>
                </el-form>
            </div>

            <!-- 右侧欢迎区域 -->
            <div class="split-welcome-section">
                <div class="split-welcome-content">
                    <h1 class="split-welcome-title">欢迎回来</h1>
                    <p class="split-welcome-text">登录您的账号, 开启全新的体验</p>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, nextTick } from 'vue';
import { ElMessage } from 'element-plus';
import { User, Lock, View, Hide } from '@element-plus/icons-vue';
import axios from "axios";
import router from "@/router/index.js";

const loginForm = ref({
    username: '',
    password: '',
    type: 1, // 默认选中管理员
});
const loading = ref(false);
const passwordType = ref('password');
const loginFormRef = ref();

const rules = {
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
    ],
};

const showPwd = () => {
    passwordType.value = passwordType.value === 'password' ? '' : 'password';
    nextTick(() => {
        // 保持原有的焦点逻辑（如果需要）
        const passwordInputEl = loginFormRef.value?.password;
        if (passwordInputEl && typeof passwordInputEl.focus === 'function') {
            passwordInputEl.focus();
        }
    });
};

const handleLogin = () => {
    loginFormRef.value.validate((valid) => {
        if (valid) {
            loading.value = true;
            axios.post("/api/user/login",loginForm.value).then(res=>{
              // console.log(res.data)
              if (res.data.code == 200){
                localStorage.setItem("token",res.data.data.token)
                // console.log(res.data.data.token)
                ElMessage.success('登录成功！');
                router.push("/")
              }else{
                ElMessage.error('登录失败！');
              }
              loading.value = false;
            })


        } else {
            ElMessage.error('请检查输入项');
            return false;
        }
    });
};
</script>

<style scoped>
.split-login-page-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background-color: #f0f2f5; /* 页面背景色 */
    padding: 20px;
    box-sizing: border-box;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

.split-login-wrapper {
    width: 980px; /* 调整总宽度以容纳两边内容 */
    height: 600px; /* 调整总高度 */
    display: flex;
    border-radius: 12px;
    box-shadow: 0 12px 35px rgba(0, 0, 0, 0.12);
    overflow: hidden;
    background-color: #fff; /* 左侧表单区域默认背景 */
}

/* 左侧表单区域 */
.split-login-form-section {
    flex: 1.2; /* 左侧可以稍微宽一点以容纳表单 */
    padding: 50px 60px;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.form-title-container {
    text-align: left;
    margin-bottom: 35px;
}

.form-title-text {
    font-size: 26px;
    color: #333;
    font-weight: 500;
}

.retained-login-form .el-form-item {
    margin-bottom: 22px; /* 调整表单项间距 */
}

/* 输入框样式 */
:deep(.retained-login-form .el-input__wrapper) {
    border-radius: 6px !important;
    background-color: #f7f8fa !important; /* 浅色背景 */
    box-shadow: none !important; /* 移除默认阴影 */
    border: 1px solid #e9e9e9 !important;
}
:deep(.retained-login-form .el-input__wrapper.is-focus) {
    border-color: #4a89ff !important;
    background-color: #fff !important;
}
:deep(.retained-login-form .el-input__inner) {
    height: 46px; /* 调整输入框高度 */
    line-height: 46px;
    font-size: 15px;
}
:deep(.retained-login-form .el-input__prefix .el-icon) {
    color: #888;
}

.show-pwd-icon {
    cursor: pointer;
    color: #888;
    font-size: 16px;
}

/* 单选框组样式 */
.user-type-radio-group {
    margin-bottom: 25px !important;
}
:deep(.user-type-radio-group .el-radio-group) {
    display: flex;
    justify-content: flex-start; /* 左对齐 */
}
:deep(.user-type-radio-group .el-radio) {
    margin-right: 20px !important;
    color: #555;
}
:deep(.user-type-radio-group .el-radio__input.is-checked .el-radio__inner) {
    border-color: #4a89ff;
    background: #4a89ff;
}
:deep(.user-type-radio-group .el-radio__input.is-checked+.el-radio__label) {
    color: #4a89ff;
}

/* 登录按钮样式 */
.retained-login-button {
    width: 100%;
    border-radius: 6px;
    font-size: 16px;
    background-color: #4a89ff; /* 主题蓝色 */
    border-color: #4a89ff;
    padding-top: 12px; /* 增加按钮高度 */
    padding-bottom: 12px;
}

.retained-login-button:hover {
    background-color: #3a75e6;
    border-color: #3a75e6;
}

/* 右侧欢迎区域 */
.split-welcome-section {
    flex: 1;
    background-color: #5d9cec; /* 图片中的蓝色 */
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 40px;
    color: white;
    text-align: center;
    border-top-right-radius: 12px;
    border-bottom-right-radius: 12px;
}

.split-welcome-content {
    max-width: 320px;
}

.split-welcome-title {
    font-size: 38px;
    font-weight: 600;
    margin-bottom: 20px;
}

.split-welcome-text {
    font-size: 17px;
    line-height: 1.7;
    opacity: 0.9;
}

/* 响应式调整 */
@media (max-width: 992px) {
    .split-login-wrapper {
        width: 90%;
        flex-direction: column;
        height: auto;
    }
    .split-welcome-section {
        border-top-right-radius: 0;
        border-bottom-left-radius: 12px;
        padding: 50px 20px;
        min-height: 280px;
    }
    .split-login-form-section {
        padding: 40px 30px;
        flex: auto; /* 移除flex比例 */
    }
}

@media (max-width: 576px) {
    .form-title-text {
        font-size: 22px;
    }
    .split-welcome-title {
        font-size: 30px;
    }
    .split-welcome-text {
        font-size: 15px;
    }
    .split-login-form-section {
        padding: 30px 20px;
    }
    :deep(.user-type-radio-group .el-radio-group) {
        flex-direction: column; /* 小屏幕下单选框垂直排列 */
        align-items: flex-start;
    }
    :deep(.user-type-radio-group .el-radio) {
        margin-bottom: 10px; /* 垂直排列时间距 */
    }
}
</style>